package com.xiaoniu.forum.web.controller.app;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoniu.forum.annotation.Login;
import com.xiaoniu.forum.annotation.LoginUser;
import com.xiaoniu.forum.common.utils.PageUtils;
import com.xiaoniu.forum.common.utils.R;
import com.xiaoniu.forum.entity.MemberEntity;
import com.xiaoniu.forum.entity.vo.AppBaseUserVo;
import com.xiaoniu.forum.entity.vo.AppFollowFansNumVo;
import com.xiaoniu.forum.renren.common.exception.RRException;
import com.xiaoniu.forum.service.MemberFollowService;
import com.xiaoniu.forum.service.MemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * 用户相关的接口
 */
@RestController
@RequestMapping("/app/user")
public class AppMemberController {
    @Autowired
    private MemberService memberService;
    @Login
    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser MemberEntity user){
        return R.ok().put("user", user);
    }


    /**
     * 更新个人资料
     */
    @Login
    @PostMapping("/update")
    @ApiOperation("更新个人资料")
    public R update(@LoginUser MemberEntity user,@RequestBody AppBaseUserVo baseUser){
        MemberEntity member = new MemberEntity();
        member.setId(user.getId());
        BeanUtil.copyProperties(baseUser,member);
        memberService.updateById(member);
        return R.ok();
    }

    /**
     * 查询用户的基础信息（昵称、头像、个人简介、性别、地址等等）
     */
    @GetMapping("/baseInfo/{id}")
    @ApiOperation("查询用户基础信息")
    public R baseInfo(@PathVariable String id){
        MemberEntity user = memberService.getById(id);
        if (user == null){
            throw new RRException("用户不存在");
        }
        AppBaseUserVo baseUserVo = new AppBaseUserVo();
        BeanUtil.copyProperties(user,baseUserVo);
        return R.ok().put("user",baseUserVo);
    }

    /**
     * 根据id批量查询用户基础信息
     */
    @ApiOperation("根据id批量查询用户基础信息")
    @PostMapping("/batch/baseInfo")
    public R batchQuery(@RequestBody List<String>ids){
        if (ids.size()==0){
            return R.ok();
        }
        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","nickname","avatar","description");
        queryWrapper.in("id",ids);
        List<MemberEntity> users = memberService.list(queryWrapper);
        List<AppBaseUserVo> baseUserVos = new ArrayList();
        for (MemberEntity user:users){
            AppBaseUserVo baseUserVo = new AppBaseUserVo();
            BeanUtil.copyProperties(user,baseUserVo);
            baseUserVos.add(baseUserVo);
        }
        return R.ok().put("data",baseUserVos);
    }
}
